package com.goodmean.teacher.admin;
import com.goodmean.common.model.Student;
import com.goodmean.common.model.Teacher;
import com.goodmean.student.StudentService;
import com.goodmean.teacher.teacher.TeacherService;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;

public class AdminController extends Controller {
	private TeacherService teacherService = TeacherService.me;
	private StudentService studentService = StudentService.me;
	/**
	 * 渲染管理员界面
	 */
	public void index(){
		render("admin_index.html");
	}

	/**
	 * 渲染教师管理页面，查询全部教师信息
	 */
	public void teacherManage(){
		int page = getParaToInt("page",1);
		String teacherName = getPara("teacher_name");
		Integer teacherId = getParaToInt("teacher_id");
		setAttr("teacher_page", teacherService.findTeacher(teacherId, teacherName, page));
		render("teacher_manage.html");
	}

	/**
	 * 添加教师信息
	 */
	public void teacherAdd() {
		int value = getParaToInt("value",1);
		switch (value) {
			case (0): render("teacher_manage_save.html"); break;
			case (1):   Teacher teacher = getModel(Teacher.class, "");
				if(teacherService.addTeacher(teacher)) {
					redirect("/admin/teacherManage");
				} break;
		}
	}

	/**
	 * 渲染教师信息更新页面
	 */
	public void teacherUpdate() {
		Integer teacherId = getParaToInt("teacher_id");
		int value = getParaToInt("value",1);
		switch (value) {
			case (0):   setAttr("teacher", teacherService.findById(teacherId));
				render("teacher_manage_update.html"); break;
			case (1):   Teacher teacher = getModel(Teacher.class, "");
				if(teacherService.updateTeacher(teacher)) {
					redirect("/admin/teacherManage");
				} break;
		}
	}

	/**
	 * 删除教师信息
	 */
	public void teacherDelete() {
		Integer teacherId = getParaToInt("teacher_id");
		if(teacherService.deleteTeacher(teacherId)) {
			redirect("/admin/teacherManage");
		}
	}

	/**
	 * 渲染学生管理页面，查询全部学生信息
	 */
	public void studentManage(){
		String studentId = getPara("student_id");
		String studentName = getPara("student_name");
		int page = getParaToInt("page",1);
		setAttr("student_page", studentService.findStudent(studentId, studentName, page));
		render("student_manage.html");
	}

	/**
	 * 渲染学生信息添加页面
	 */
	public void studentAdd() {
		int value = getParaToInt("value",1);
		switch (value) {
			case (0):   render("student_manage_save.html"); break;
			case (1):   Student student = getModel(Student.class, "");
				if(studentService.addStudent(student)) {
					redirect("/admin/studentManage");
				} break;
		}
	}

	/**
	 * 渲染学生信息更新页面
	 */
	public void studentUpdate() {
		String studentId = getPara("student_id");
		int value = getParaToInt("value",1);
		switch (value) {
			case (0):   setAttr("student", studentService.findByid(studentId));
				render("student_manage_update.html"); break;
			case (1):   Student student = getModel(Student.class, "");
				if(studentService.updateStudent(student)) {
					redirect("/admin/studentManage");
				} break;
		}
	}
	/**
	 * 删除学生信息
	 */
	public void studentDelete() {
		String studentId = getPara("student_id");
		if(studentService.deleteStudent(studentId)) {
			redirect("/admin/studentManage");
		}
	}

}